package com.sls.game.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.PriceTemplateDAO;
import com.sls.game.dao.SpecialPriceSystemDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.PriceTemplateInfo;
import com.sls.game.pojo.SpecialPriceSystemInfo;
import com.sls.game.querybean.PriceTemplateQuery;
import com.sls.game.service.PriceTemplateService;

@Service("priceTemplateService")
public class PriceTemplateServiceImpl implements PriceTemplateService {
	private static Logger log = Logger
			.getLogger(PriceTemplateServiceImpl.class);
	@Resource(name = "priceTemplateDAO")
	private PriceTemplateDAO priceTemplateDAO;
	@Resource(name = "specialPriceSystemDAO")
	private SpecialPriceSystemDAO specialDAO;
	private Gson gson;

	public PriceTemplateServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public String save(String body, Head head) throws ServerDisponseException {
		PriceTemplateInfo templateInfo = gson.fromJson(body,
				PriceTemplateInfo.class);
		this.validate(templateInfo);
		if (StringUtils.isEmpty(templateInfo.getTemplateName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TEMPLATE_NAME_EMPTY.getCode(),
					ExceptionCodeDes.TEMPLATE_NAME_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(templateInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		this.templateExistForSave(templateInfo);
		templateInfo.setId(UUID.randomUUID().toString());
		templateInfo.setTemplateCode(this.templateCodeGenerator());
		templateInfo.setInsertTime(ServerDateAndTime.getNowDateTimeOfFormat());

		try {
			this.priceTemplateDAO.insert(templateInfo);
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 模板编号产生器
	 * 
	 * @return
	 */
	private String templateCodeGenerator() {
		String templateCode = "";
		String maxCode = this.priceTemplateDAO.queryMaxTemplateCode();

		if (StringUtils.isEmpty(maxCode)) {
			templateCode = "1000000";
		} else {
			int code = Integer.parseInt(maxCode);
			templateCode = String.valueOf(code + 1);
		}
		return templateCode;
	}

	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		PriceTemplateInfo templateInfo = gson.fromJson(body,
				PriceTemplateInfo.class);
		this.validate(templateInfo);
		this.validateField(templateInfo);
		this.templateExistForUpdate(templateInfo);
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		templateInfo.setModifyTime(modifyTime);
		SpecialPriceSystemInfo specialInfo = new SpecialPriceSystemInfo();
		specialInfo.setTemplateCode(templateInfo.getTemplateCode());
		specialInfo.setTemplateName(templateInfo.getTemplateName());
		specialInfo.setModifyTime(modifyTime);
		specialInfo.setOperator(templateInfo.getOperator());
		try {
			this.priceTemplateDAO.update(templateInfo);
			this.specialDAO.updateTemplateName(specialInfo);
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 校验模板是否已存在
	 * 
	 * @param templateInfo
	 */
	private void templateExistForUpdate(PriceTemplateInfo templateInfo) {
		String templateCode = "";
		try {
			templateCode = this.priceTemplateDAO
					.queryTemplateCodeByName(templateInfo.getTemplateName());
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		String code = templateInfo.getTemplateCode();
		if (StringUtils.isNotEmpty(templateCode)) {
			if (!code.equals(templateCode)) {
				throw new ServerDisponseException(
						ExceptionCodeDes.TEMPLATE_NAME_EXIST.getCode(),
						ExceptionCodeDes.TEMPLATE_NAME_EXIST.getDescription());
			}
		} else {

		}
	}

	private void templateExistForSave(PriceTemplateInfo templateInfo) {
		String templateCode = "";
		try {
			templateCode = this.priceTemplateDAO
					.queryTemplateCodeByName(templateInfo.getTemplateName());
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		if (StringUtils.isNotEmpty(templateCode)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TEMPLATE_NAME_EXIST.getCode(),
					ExceptionCodeDes.TEMPLATE_NAME_EXIST.getDescription());
		}
	}

	@Override
	public String queryTemplateByCode(String body, Head head)
			throws ServerDisponseException {
		PriceTemplateInfo templateInfo = gson.fromJson(body,
				PriceTemplateInfo.class);
		this.validate(templateInfo);
		String code = templateInfo.getTemplateCode();
		try {
			templateInfo = this.priceTemplateDAO.queryTemplateByCode(code);
			if (templateInfo != null) {
				return gson.toJson(templateInfo);
			}
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String queryTemplateNameList(Head head)
			throws ServerDisponseException {
		try {
			List<String> templateNames = this.priceTemplateDAO
					.queryTemplateNameList();
			if (templateNames != null) {
				return gson.toJson(templateNames);
			}
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String queryPriceTemplate(String body, Head head)
			throws ServerDisponseException {
		PriceTemplateQuery templateQuery = gson.fromJson(body,
				PriceTemplateQuery.class);
		this.validate(templateQuery);

		try {
			Pagination<PriceTemplateInfo> page = priceTemplateDAO
					.queryPriceTemplate(templateQuery);
			if (page != null) {
				return gson.toJson(page);
			}
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 校验是否为null
	 * 
	 * @param priceInfo
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	/**
	 * 校验字段是否为空
	 * 
	 * @param templateInfo
	 * @return
	 */
	private boolean validateField(PriceTemplateInfo templateInfo)
			throws ServerDisponseException {
		boolean result = false;
		if (StringUtils.isEmpty(templateInfo.getTemplateCode())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TEMPLATE_ID_EMPTY.getCode(),
					ExceptionCodeDes.TEMPLATE_ID_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(templateInfo.getTemplateName())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TEMPLATE_NAME_EMPTY.getCode(),
					ExceptionCodeDes.TEMPLATE_NAME_EMPTY.getDescription());
		} else if (StringUtils.isEmpty(templateInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		} else {
			result = true;
		}
		return result;
	}

	@Override
	public List<PriceTemplateInfo> queryTemplateList(Head head)
			throws ServerDisponseException {
		List<PriceTemplateInfo> list = this.priceTemplateDAO.queryTemplateList();
		return list;
	}

}
